/**
 *
 * 版权所有 © cybersky.com
 *
 */
package cn.cybertron.primus.report.schedule;

import java.util.Properties;
import java.util.UUID;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.NutConfig;

import cn.cybertron.primus.appservice.AbstractAppService;
import cn.cybertron.primus.base.RepeatNameException;
import cn.cybertron.primus.report.entity.ReportObject;
import cn.cybertron.primus.schedule.Scheduler;

/**
 * 报表调度管理器，负责报表的调度生成文件，单例
 * @author ch
 *
 */
public class ReportScheduleManager extends AbstractAppService {
	
	private static final Log logger = Logs.get();

	private Properties schedulerConfig;

	private static Scheduler scheduler;
	
	private static final ReportScheduleManager me = new ReportScheduleManager();
	
	public static final ReportScheduleManager me() {
		return me;
	}
	
	/**
	 * 构造函数
	 */
	private ReportScheduleManager() {
		
	}
	
	public Properties getSchedulerConfig() {
		return schedulerConfig;
	}

	public void setSchedulerConfig(Properties schedulerConfig) {
		this.schedulerConfig = schedulerConfig;
	}

	/**
	 * 加载时即读取报表和报告的调度信息
	 */
	@Override
	public void init(NutConfig config) {
		synchronized (this) {
			if (scheduler == null)
				scheduler = Scheduler.createScheduler(schedulerConfig);
		}
	}

	@Override
	public void destroy(NutConfig config) {
		
	}
	
	/**
	 * 添加报表创建中间表
	 * @param report
	 * @return
	 * @throws RepeatNameException
	 *  判断是否存在同名报表
	 */
	public String addReport(ReportObject report) throws RepeatNameException {
		try {
			String uuid = UUID.randomUUID().toString();
			if(report != null) {
				report.setUuid(uuid);
				
			}
			return uuid;
		} catch (Exception e) {
			logger.error("", e);
			throw new RepeatNameException();
		}
	}
	
}

